package com.huike.controller;

import com.huike.pojo.SysUser;
import com.huike.service.SystemService;
import com.huike.vo.ImageVerificationCode;
import com.huike.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

@Controller
@RequestMapping("/system")
@ResponseBody
public class SystemController {
    @Autowired
    private SystemService systemService;

    /**
     * 系统登录
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ResultVO<SysUser> login(String userName, String password, HttpSession session){
        SysUser sysUser = systemService.login(userName, password);
        if (sysUser==null){
            ResultVO resultVO = new ResultVO(100, "用户名或密码错误，请检查！");
            return resultVO;
        }else{
            if (sysUser.getStatus()==1){
                return new ResultVO(300,"此用户已被禁用，请联系管理员！");
            }else {
                session.setAttribute("loginName",sysUser.getLoginName());
                ResultVO<SysUser> sysUserResultVO = new ResultVO<>();
                sysUserResultVO.setObj(sysUser);
                return sysUserResultVO;
            }
        }
    }

    /**
     * 获取随机验证码
     * @return
     */
    @RequestMapping(value = "random",method = RequestMethod.GET)
    public Integer random(){
        Random random=new Random();
        int x=random.nextInt(9000)+1000;
        return x;
    }
    @RequestMapping("getVerifiCode")
    public void getVerifiCode(HttpServletRequest request, HttpServletResponse response,HttpSession session) throws IOException {
        /*
             1.生成验证码
             2.把验证码上的文本存在session中
             3.把验证码图片发送给客户端
             */

        ImageVerificationCode ivc = new ImageVerificationCode();     //用我们的验证码类，生成验证码类对象
        BufferedImage image = ivc.getImage();  //获取验证码
        session.setAttribute("text", ivc.getText());//将验证码的文本存在session中
        System.out.println("1111111111111/"+session.getAttribute("text"));
        ivc.output(image, response.getOutputStream());//将验证码图片响应给客户端
    }
    @RequestMapping("loginAuthentication")
    @ResponseBody
    public String Login_authentication(HttpServletRequest request,HttpServletResponse response,HttpSession session) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        String session_vcode=(String) session.getAttribute("text");//从session中获取真正的验证码
        System.out.println("222222222222/"+session_vcode);
        return session_vcode;
    }
    /**
     * 用户退出
     * @param session
     * @return
     */
    @RequestMapping(value = "logout",method = RequestMethod.GET)
    public ResultVO out(HttpSession session){
        session.invalidate();
        return new ResultVO();
    }
}
